// index.js
wx.cloud.init()
const db = wx.cloud.database()
Page({
    data: {
        banner: [],
        navCate: [{
                name: '才艺',
                src: '../../images/talent.png',
                type: 'index',
                to: '/pages/Cate/Cate'
            },
            {
                name: '团队',
                src: '../../images/tram.png',
                type: 'index',
                to: '/pages/car/car'
            },
            {
                name: '行程',
                src: '../../images/stroke.png',
                type: 'index',
                to: '/pages/me/me'
            },
            {
                name: '联系',
                src: '../../images/contact.png',
                type: 'page',
                to: '/pages/lianxi/lianxi'
            },
        ],
        shopList: [],
        newShopList: [],
        search: [],
        searchText: "",
        num: 0,

    },
    // 跳转
    topage(e) {
        if (e.currentTarget.dataset.type.type === 'page') {
            wx.navigateTo({
                url: e.currentTarget.dataset.type.to
            })
        } else {
            wx.switchTab({
                url: e.currentTarget.dataset.type.to
            });
        }
    },
    // 搜索
    search(e) {
        let that = this
        wx.navigateTo({
            url: '/pages/productDetail/productDetail?searchText=' + e.detail.value.trim()
        })
        this.setData({
            searchText: ""
        })
    },
    // 初始化
    onLoad() {
        let that = this
        // 轮播图图片获取
        db.collection('swiper').get({
            success: function (res) {
                that.setData({
                    banner: res.data
                })
            },
            fail: function (res) {}
        })
        // 首页列表
        db.collection('product').skip(that.data.num).limit(10).get({
            success: function (res) {
                that.setData({
                    shopList: res.data
                })
            },
            fail: function (res) {}
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let that = this
        wx.showLoading({
            title: '加载中',
            duration: 1000
        })
        that.setData({
            num: (that.data.num + 10)
        })
        console.log(that.data.num);
        db.collection('product').skip(that.data.num).limit(10).get({
            success: function (res) {
                that.setData({
                    shopList: that.data.shopList.concat(res.data)
                })
                console.log(that.data.shopList.length);
                console.log(that.data.num);
                console.log(parseInt(Math.random() * (that.data.shopList.length - 1) + that.data.num));
                for (let i = that.data.num, len = that.data.shopList.length; i < len; i++) {
                    let currentRandom = parseInt(Math.random() * (that.data.shopList.length - 1 - that.data.num) + that.data.num);
                    let current = that.data.shopList[i];
                    that.data.shopList[i] = that.data.shopList[currentRandom];
                    that.data.shopList[currentRandom] = current;
                }
                that.setData({
                    shopList: that.data.shopList
                })
            },
            fail: function (res) {}
        })
    },


})